#include "socketServerThread.h"
#include "socketServerData.h"
#include "constants.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include "dnsPacket.h"
#include "helper.h"

void *socketServerThread( void *data )
{
	SocketServerData *serverData = static_cast<SocketServerData*>(data);
	SocketServer socketServer(serverData); 
	socketServer.run();
	return NULL;
}

SocketServer::SocketServer( SocketServerData *data )
{
	data_ = data;
}

SocketServer::~SocketServer()
{

}

void SocketServer::run()
{
	server_.setPortNum( ss_itoa( data_->listenPort() ));
	server_.setListenAddress( data_->listenAddress() );

	while( true )
	{
		UDPMessage request = server_.incommingMessage();
		DNSPacket dnsRequest( request.data() );
		DNSQuestion question = dnsRequest.question( 0 );
		data_->verbose( request.source().address() + "\t Requested " + question.queryName() );
	}
}

